from socket import *
import threading




color_int = dict()

color_int['red'] = 1
color_int['green'] = 2
color_int['no_move'] = 3

buffer_data = str()

buffer_test = "3333"

class ProcessTest(threading.Thread):
    def __init__(self, rtmp_socket):
        super(ProcessTest, self).__init__()
        self.rtmp_socket = rtmp_socket        # rtmp的套接字
    def run(self):
        while True:
            data = self.rtmp_socket.recv(8192)
            data = data.decode('utf-8')
            if data == '':
                continue
            global buffer_data
            buffer_data = buffer_data+str(color_int[data])
            self.rtmp_socket.send(str("OK").encode("utf-8"))


class ProcessAndroid(threading.Thread):
    def __init__(self, android_socket):
        super(ProcessAndroid, self).__init__()
        self.android_socket = android_socket        # rtmp的套接字

    def run(self):
        while True:
            print("from android")
            global buffer_data
            self.android_socket.send(buffer_test.encode("utf-8"))




if __name__=="__main__":

    # IP = gethostbyname(gethostname())
    # print("本机IP: ", IP)
    ip = '192.168.43.65'
    ADDR = (ip, 12346)
    # BUFSIZ = 1024

    s = socket(AF_INET, SOCK_STREAM)
    s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    s.bind(ADDR)
    s.listen(5)

    while True:
        print('waiting for clients ...')
        c, addr = s.accept()
        if addr[0] == "192.168.43.65":
            print("from test")
            pt = ProcessTest(c)
            pt.daemon = True
            pt.start()
        elif addr[0] == "192.168.43.1":
            pa = ProcessAndroid(c)
            pa.daemon = True
            pa.start()
        else:
            continue
    s.close()